package com.adams.custom.page.core.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.adams.access.db.bean.ExecSqlScriptInfo;
import com.adams.access.db.bean.SystemVersion;
import com.adams.config.engine.PropertesEngine;
import com.adams.core.exception.BusinessException;
import com.adams.custom.page.core.dao.impl.ExecSqlScriptInfoDaoImpl;
import com.adams.custom.page.core.dao.impl.SystemVersionDaoImpl;
import com.adams.custom.page.core.service.ExecSqlScriptInfoService;
import com.adams.custom.page.core.service.SystemVersionService;
import com.adams.custom.page.util.ViewUtil;
import com.adams.utils.SQLScriptUtil;

/**
 * 
 * File Name : ExecSqlScriptInfoServiceImpl.java
 * 
 * @Description : 执行数据库脚本信息服务层
 * @author 904370 张汉辉
 */
public class ExecSqlScriptInfoServiceImpl implements ExecSqlScriptInfoService,
		SystemVersionService
{
	private static Logger logger = Logger
			.getLogger(ExecSqlScriptInfoServiceImpl.class.getName());

	private static String JDBC_CONFIG_FILE_NAME = "jdbc.properties";
	private static Properties jdbcProperties = new Properties();
	static
	{
		InputStream is = PropertesEngine.class.getClassLoader()
				.getResourceAsStream(JDBC_CONFIG_FILE_NAME);
		try
		{
			jdbcProperties.load(is);
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			logger.error("读取配置文件" + JDBC_CONFIG_FILE_NAME + "出错", e);
		}
	}
	/**
	 * Description : 默认的数据库驱动 Author : 904370
	 */
	private String defaultDriverClassName = "";
	/**
	 * Description : 默认的数据库连接 Author : 904370
	 */
	private String defaultUrl = "";
	/**
	 * Description : 默认的数据库用户名 Author : 904370
	 */
	private String defaultUser = "";
	/**
	 * Description : 默认的数据库密码 Author : 904370
	 */
	private String defaultPassword = "";

	/**
	 * Description : 执行数据库脚本数据访问层 Author : 904370
	 */
	private ExecSqlScriptInfoDaoImpl execSqlScriptInfoDaoImpl;

	/**
	 * Description : 系统版本信息数据访问类 Author : 904370
	 */
	private SystemVersionDaoImpl systemVersionDaoImpl;

	public void setSystemVersionDaoImpl(
			SystemVersionDaoImpl systemVersionDaoImpl)
	{
		this.systemVersionDaoImpl = systemVersionDaoImpl;
	}

	public void setExecSqlScriptInfoDaoImpl(
			ExecSqlScriptInfoDaoImpl execSqlScriptInfoDaoImpl)
	{
		this.execSqlScriptInfoDaoImpl = execSqlScriptInfoDaoImpl;
	}

	public void setDefaultDriverClassName(String defaultDriverClassName)
	{
		this.defaultDriverClassName = defaultDriverClassName;
	}

	public void setDefaultUrl(String defaultUrl)
	{
		this.defaultUrl = defaultUrl;
	}

	public void setDefaultUser(String defaultUser)
	{
		this.defaultUser = defaultUser;
	}

	public void setDefaultPassword(String defaultPassword)
	{
		this.defaultPassword = defaultPassword;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seecom.adams.custom.page.core.service.ExecSqlScriptInfoService#
	 * findAllExecSqlScriptInfo ()
	 */
	public List<ExecSqlScriptInfo> findAllExecSqlScriptInfo()
	{
		return execSqlScriptInfoDaoImpl.findAll();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seecom.adams.custom.page.core.service.ExecSqlScriptInfoService#
	 * saveOrUpdateExecSqlScriptInfo
	 * (com.adams.access.db.bean.ExecSqlScriptInfo, java.lang.String)
	 */
	public ExecSqlScriptInfo saveOrUpdateExecSqlScriptInfo(
			ExecSqlScriptInfo entity, String sqlScript)
			throws BusinessException
	{
		ExecSqlScriptInfo src = entity;
		if (null != entity.getId())
		{
			src = execSqlScriptInfoDaoImpl.get(entity.getId());
			src.setBusinessname(entity.getBusinessname());
			src.setInitsql(entity.getInitsql());
			src.setAdamsinstallsql(entity.getAdamsinstallsql());
			src.setAdamsuninstallsql(entity.getAdamsuninstallsql());
		}

		execSqlScriptInfoDaoImpl.saveOrUpdate(src);
		if (!ViewUtil.isNuLLStr(sqlScript))
		{
			String driverClassName = getDriverClassName(entity
					.getBusinessname(), false);
			Properties properties = getJdbcConnectionProperties(entity
					.getBusinessname(), false);
			SQLScriptUtil.ExecuteSqlScript(driverClassName, properties,
					sqlScript);
		}
		return src;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.adams.custom.page.core.service.ExecSqlScriptInfoService#delete
	 * (com.adams.access.db.bean.ExecSqlScriptInfo)
	 */
	public void deleteExecSqlScriptInfo(ExecSqlScriptInfo entity)
			throws BusinessException
	{
		if (null == entity.getId())
		{
			return;
		}
		logger.info("删除ExceSqlScriptInfo id:" + entity.getId()
				+ " businessname:" + entity.getBusinessname());
		String sqlScript = entity.getAdamsuninstallsql();
		ExecSqlScriptInfo src = execSqlScriptInfoDaoImpl.get(entity.getId());
		execSqlScriptInfoDaoImpl.delete(src);
		if (!ViewUtil.isNuLLStr(sqlScript))
		{
			String driverClassName = getDriverClassName(entity
					.getBusinessname(), true);
			Properties properties = getJdbcConnectionProperties(entity
					.getBusinessname(), true);
			SQLScriptUtil.ExecuteSqlScript(driverClassName, properties,
					sqlScript);
		}
	}

	private String getDriverClassName(String bussinessname, boolean isDefault)
			throws BusinessException
	{
		String result = defaultDriverClassName;

		if (isDefault || ViewUtil.isNuLLStr(bussinessname))
		{
			return result;
		}
		String key = bussinessname + ".jdbc.driverClassName";
		String driverClassName = jdbcProperties.getProperty(key);
		if (!ViewUtil.isNuLLStr(driverClassName))
		{
			result = driverClassName;
		}
		return result;
	}

	private Properties getJdbcConnectionProperties(String bussinessname,
			boolean isDefault) throws BusinessException
	{
		Properties result = null;
		if (!isDefault && ViewUtil.isNuLLStr(bussinessname))
		{
			String key = bussinessname + ".jdbc.url";
			String url = jdbcProperties.getProperty(key);
			key = bussinessname + ".jdbc.username";
			String user = jdbcProperties.getProperty(key);
			key = bussinessname + ".jdbc.password";
			String password = jdbcProperties.getProperty(key);
			result = getJdbcConnectionProperties(url, user, password);
		}

		if (null == result)
		{
			result = getJdbcConnectionProperties(defaultUrl, defaultUser,
					defaultPassword);
		}

		return result;
	}

	private Properties getJdbcConnectionProperties(String url, String user,
			String password)
	{
		Properties result = null;
		if (!ViewUtil.isNuLLStr(url) && !ViewUtil.isNuLLStr(user)
				&& !ViewUtil.isNuLLStr(password))
		{
			result = new Properties();
			result.put("url", url);
			result.put("user", user);
			result.put("password", password);
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seecom.adams.custom.page.core.service.SystemVersionService#
	 * findAllSystemVersion()
	 */
	@Override
	public List<SystemVersion> findAllSystemVersion()
	{
		// TODO Auto-generated method stub
		return systemVersionDaoImpl.findAll();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seecom.adams.custom.page.core.service.SystemVersionService#
	 * saveOrUpdateSystemVersion(com.adams.access.db.bean.SystemVersion,
	 * java.lang.String)
	 */
	public SystemVersion saveOrUpdateSystemVersion(SystemVersion entity,
			String sqlScript) throws BusinessException
	{
		// TODO Auto-generated method stub
		systemVersionDaoImpl.saveOrUpdate(entity);
		if (!ViewUtil.isNuLLStr(sqlScript))
		{
			String driverClassName = getDriverClassName("", true);
			Properties properties = getJdbcConnectionProperties("", true);
			SQLScriptUtil.ExecuteSqlScript(driverClassName, properties,
					sqlScript);
		}
		return entity;
	}
}
